home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / LogStream.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  207 lines

  1. /*
  2.  * @(#)LogStream.java    1.8 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi.server;
  15.  
  16. import java.io.*;
  17. import java.util.*;
  18.  
  19. /**
  20.  * <code>LogStream</code> presents a mechanism for logging errors that are
  21.  * of possible interest to those monitoring the system.
  22.  *
  23.  * @author Ann Wollrath (lots of code stolen from Ken Arnold)
  24.  */
  25. public class LogStream extends PrintStream {
  26.  
  27.     /** table mapping known log names to log stream objects */
  28.     private static Hashtable    known = new Hashtable(5);
  29.     /** default output stream for new logs */
  30.     private static PrintStream    defaultStream = System.err;
  31.  
  32.     /** log name for this log */
  33.     private String name;
  34.  
  35.     /** stream where output of this log is sent to */
  36.     private OutputStream logOut;
  37.  
  38.     /** string writer for writing message prefixes to log stream */
  39.     private OutputStreamWriter logWriter;
  40.  
  41.     /** string buffer used for constructing log message prefixes */
  42.     private StringBuffer buffer = new StringBuffer();
  43.  
  44.     /** stream used for buffering lines */
  45.     private ByteArrayOutputStream bufOut;
  46.  
  47.     /**
  48.      * Create a new LogStream object.  Since this only constructor is
  49.      * private, users must have a LogStream created through the "log"
  50.      * method.
  51.      * @param name string identifying messages from this log
  52.      * @out output stream that log messages will be sent to
  53.      */
  54.     private LogStream(String name, OutputStream out)
  55.     {
  56.     super(new ByteArrayOutputStream());
  57.     bufOut = (ByteArrayOutputStream) super.out;
  58.  
  59.     this.name = name;
  60.     setOutputStream(out);
  61.     }
  62.  
  63.     /**
  64.      * Return the LogStream identified by the given name.  If
  65.      * a log corresponding to "name" does not exist, a log using
  66.      * the default stream is created.
  67.      */
  68.     public static LogStream log(String name) {
  69.     LogStream stream;
  70.     synchronized (known) {
  71.         stream = (LogStream)known.get(name);
  72.         if (stream == null) {
  73.         stream = new LogStream(name, defaultStream);
  74.         }
  75.         known.put(name, stream);
  76.     }
  77.     return stream;
  78.     }
  79.  
  80.     /**
  81.      * Return the current default stream for new logs.
  82.      */
  83.     public static synchronized PrintStream getDefaultStream() {
  84.     return defaultStream;
  85.     }
  86.  
  87.     /**
  88.      * Set the default stream for new logs.
  89.      */
  90.     public static synchronized void setDefaultStream(PrintStream newDefault) {
  91.     defaultStream = newDefault;
  92.     }
  93.  
  94.     /**
  95.      * Return the current stream to which output from this log is sent.
  96.      */
  97.     public synchronized OutputStream getOutputStream()
  98.     {
  99.     return logOut;
  100.     }
  101.     
  102.     /**
  103.      * Set the stream to which output from this log is sent.
  104.      */
  105.     public synchronized void setOutputStream(OutputStream out)
  106.     {
  107.     logOut = out;
  108.     // Maintain an OutputStreamWriter with default CharToByteConvertor
  109.     // (just like new PrintStream) for writing log message prefixes.
  110.     logWriter = new OutputStreamWriter(logOut);
  111.     }
  112.     
  113.     /**
  114.      * Write a byte of data to the stream.  If it is not a newline, then
  115.      * the byte is appended to the internal buffer.  If it is a newline,
  116.      * then the currently buffered line is sent to the log's output
  117.      * stream, prefixed with the appropriate logging information.
  118.      */
  119.     public void write(int b)
  120.     {
  121.     if (b == '\n') {
  122.         // synchronize on "this" first to avoid potential deadlock
  123.         synchronized (this) {
  124.         synchronized (logOut) {
  125.             // construct prefix for log messages:
  126.             buffer.setLength(0);;
  127.             buffer.append(        // date/time stamp...
  128.             (new Date()).toString());
  129.             buffer.append(':');
  130.             buffer.append(name);    // ...log name...
  131.             buffer.append(':');
  132.             buffer.append(Thread.currentThread().getName());
  133.             buffer.append(':');    // ...and thread name
  134.  
  135.             try {
  136.             // write prefix through to underlying byte stream
  137.             logWriter.write(buffer.toString());
  138.             logWriter.flush();
  139.  
  140.             // finally, write the already converted bytes of
  141.             // the log message
  142.             bufOut.writeTo(logOut);
  143.             logOut.write(b);
  144.             logOut.flush();
  145.             } catch (IOException e) {
  146.             setError();
  147.             } finally {
  148.             bufOut.reset();
  149.             }
  150.         }
  151.         }
  152.     }
  153.     else
  154.         super.write(b);
  155.     }
  156.  
  157.     /**
  158.      * Write a subarray of bytes.  Pass each through write byte method.
  159.      */
  160.     public void write(byte b[], int off, int len)
  161.     {
  162.     if (len < 0)
  163.         throw new ArrayIndexOutOfBoundsException(len);
  164.     for (int i = 0; i < len; ++ i)
  165.         write(b[off + i]);
  166.     }
  167.  
  168.     /**
  169.      * Return log name as string representation
  170.      */
  171.     public String toString()
  172.     {
  173.     return name;
  174.     }
  175.  
  176.     /** constants for logging levels */
  177.     public static final int SILENT  = 0;
  178.     public static final int BRIEF   = 10;
  179.     public static final int VERBOSE = 20;
  180.  
  181.     /**
  182.      * Convert a string name of a logging level to its internal
  183.      * integer representation.
  184.      */
  185.     public static int parseLevel(String s)
  186.     {
  187.     if ((s == null) || (s.length() < 1))
  188.         return -1;
  189.  
  190.     try {
  191.         return Integer.parseInt(s);
  192.     } catch (NumberFormatException e) {
  193.     }
  194.     if (s.length() < 1)
  195.         return -1;
  196.  
  197.     if ("SILENT".startsWith(s.toUpperCase()))
  198.         return SILENT;
  199.     else if ("BRIEF".startsWith(s.toUpperCase()))
  200.         return BRIEF;
  201.     else if ("VERBOSE".startsWith(s.toUpperCase()))
  202.         return VERBOSE;
  203.  
  204.     return -1;
  205.     }
  206. }
  207.